package cn.koala.key.receipt.controller;

import cn.koala.key.entity.BorrowOrder;
import cn.koala.key.receipt.dto.BorrowOrderCheckDTO;
import cn.koala.key.receipt.dto.BorrowOrderDTO;
import cn.koala.key.receipt.dto.BorrowOrderSearchDTO;
import cn.koala.key.receipt.service.BorrowOrderService;
import cn.ehi.authority.annotation.WebAuthentication;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultList;
import cn.seed.web.jwt.JwtTokenAuthentication;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @description:借用单
 * @author 32415
 * @time 2019/04/28
 **/
@RestController
@RequestMapping("/borrow-orders")
@Api(value = "BorrowOrder",tags = "借用单")
public class BorrowOrderController {

    @Autowired
    private BorrowOrderService borrowOrderService;

    @ApiOperation(value = "借用单查询")
    @GetMapping()
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00002")
    public Result<ResultList<BorrowOrder>> search(BorrowOrderSearchDTO borrowOrderSearch) {
        return borrowOrderService.searchBorrowOrder(borrowOrderSearch);
    }

    @ApiOperation(value = "借用单提交")
    @PutMapping("/submit")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00004")
    public Result submit(Long id) {
        return borrowOrderService.submit(id);
    }

    @ApiOperation(value = "借用单作废")
    @PutMapping("/invalid")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00003")
    public Result invalid(Long id) {
        return borrowOrderService.invalid(id);
    }

    @ApiOperation(value = "借用单审核, 支持批量")
    @PutMapping("/check")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00005,KOALAKEY_B_00010")
    public Result check(@RequestBody List<BorrowOrderCheckDTO> borrowOrderCheckList) {
        return borrowOrderService.check(borrowOrderCheckList);
    }

    @ApiOperation(value = "借用单详情")
    @GetMapping("/{id}")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00001")
    public Result<BorrowOrder> getById(@ApiParam("借用单id") @PathVariable Long id) {
        return borrowOrderService.getById(id);
    }

    @ApiOperation(value = "借用单创建和编辑")
    @PostMapping()
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00009")
    public Result createOrEdit(@RequestBody @Valid BorrowOrderDTO borrowOrderDTO) {
        return borrowOrderService.createOrEdit(borrowOrderDTO);
    }

    @ApiOperation(value = "借用单批量创建")
    @PostMapping("/batch")
    public Result<List<BorrowOrder>> batchCreate(MultipartFile file,
                              HttpServletRequest request) {
        return borrowOrderService.batchCreate(file, JwtTokenAuthentication.getUserCode(request));
    }

}
